package com.wmx.yuanyuan.auto;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Random;

public class CsdnClick {
    private static final Logger logger = LoggerFactory.getLogger(CsdnClick.class);
    private static final int sleepTime = 2;

    /**
     * 模拟用户单击屏幕指定区域,默认单击屏幕最中央
     *
     * @param x：x坐标
     * @param y：y坐标
     */
    public static final void clickScreenByXY(Integer x, Integer y) {
        try {
            Toolkit toolkit = Toolkit.getDefaultToolkit();/**创建工具包对象*/
            Robot robot = new Robot();/**创建自动化对象*/
            x = x == null ? toolkit.getScreenSize().width / 2 : x;/**利用工具包对象获取屏幕分辨率*/
            y = y == null ? toolkit.getScreenSize().height / 2 : y;
            robot.mouseMove(x, y);//移动鼠标到指定位置
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);//然后按下鼠标左键，再松开，模拟单击操作
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            robot.delay(100);//延时操作
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public static final void rightClickScreenByXY(Integer x, Integer y) {
        try {
            Toolkit toolkit = Toolkit.getDefaultToolkit();/**创建工具包对象*/
            Robot robot = new Robot();/**创建自动化对象*/
            x = x == null ? toolkit.getScreenSize().width / 2 : x;/**利用工具包对象获取屏幕分辨率*/
            y = y == null ? toolkit.getScreenSize().height / 2 : y;
            robot.mouseMove(x, y);//移动鼠标到指定位置
            robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);//然后按下鼠标左键，再松开，模拟单击操作
            robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
            robot.delay(100);//延时操作
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟按下键盘的"Ctrl+*"，比如文档起始位置：Ctrl+Home，结束位置：Ctrl+End
     * 注意：Ctrl键是：KeyEvent.VK_CONTROL
     *
     * @param keycode：按键的值,如End键:KeyEvent.VK_END、Home键：KeyEvent.VK_HOME
     */
    public static final void pressCtrlAndSingleKeyByNumber(int keycode) {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(keycode);
            robot.keyRelease(keycode);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.delay(100);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模拟按下键盘单个按键，比如文档下一页：PgDn，上一页是PgUp等
     *
     * @param keycode：按键的值,如：KeyEvent.VK_PAGE_UP
     */
    public static final void pressSingleKeyByNumber(int keycode) {
        try {
            /** 创建自动化测试对象  */
            Robot robot = new Robot();
            /**按下按键*/
            robot.keyPress(keycode);
            /**松开按键*/
            robot.keyRelease(keycode);
            /**可以稍作延时处理*/
            robot.delay(500);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    //关闭其它标签页
    public static final void closeOtherWindow() throws InterruptedException {
        Random random = new Random();
        rightClickScreenByXY(200, 2);
        int random_t = (sleepTime + random.nextInt(2)) * 1000;
        Thread.sleep(random_t);
        pressSingleKeyByNumber(KeyEvent.VK_O);
    }

    //x [700,800],y [480,880]
    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        //1）倒计时提示开始...
        int countdown = 3;
        for (int i = countdown; i > 0; i--) {
            logger.info(i + " 秒后开始自动点击...");
            Thread.sleep(1000);
        }
        int downCount = 0;
        for (int i = 0; i < 1000; i++) {
            logger.info("开始进行第 " + (i + 1) + " 点击...");
            //2）移动鼠标并进行单击
            int random_x = 700 + random.nextInt(100);
            int random_y = 480 + random.nextInt(400);
            clickScreenByXY(random_x, random_y);

            //3）延迟一定时间按 ctrl + 1 重回列表页面.
            int random_t = (sleepTime + random.nextInt(2)) * 1000;
            Thread.sleep(random_t);
            pressCtrlAndSingleKeyByNumber(KeyEvent.VK_1);
            random_t = (sleepTime + random.nextInt(2)) * 1000;
            Thread.sleep(random_t);

            //4）点击达到一定次数后，关闭 csnd 主页以外的其它页面
            if (i % 10 == 0 && i > 0) {
                closeOtherWindow();
                pressSingleKeyByNumber(KeyEvent.VK_PAGE_DOWN);
                downCount++;
            }
            //重新回到顶部
            if (downCount % 5 == 0 && downCount > 0) {
                pressCtrlAndSingleKeyByNumber(KeyEvent.VK_HOME);
            }
        }

        logger.info("自动化程序退出...");
    }

    @org.junit.Test
    public void test1() throws InterruptedException {
        clickScreenByXY(1160, 780);
    }
}
